using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    Rigidbody2D rb;
    Vector2 movement;
    private float horizontalInput;
    private float verticalInput;
    private Animator animator;
    public float speed;//移动速度
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }
    void Update()
    {
        horizontalInput = Input.GetAxisRaw("Horizontal");
        verticalInput = Input.GetAxisRaw("Vertical");
        movement = new Vector2(horizontalInput, verticalInput).normalized;
        switchAnimation(horizontalInput, verticalInput);
    }

    private void FixedUpdate()
    {
        //移动代码
        rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
    }

    //切换动画
    void switchAnimation(float horizontalInput, float verticalInput)
    {
        //控制动画
        if (horizontalInput == 0 && verticalInput == 0)
        {
            animator.SetBool("isRun", false);
        }
        else
        {
            animator.SetBool("isRun", true);
            animator.SetFloat("Horizontal", horizontalInput);
            animator.SetFloat("Vertical", verticalInput);
        }
        if(Input.GetKeyDown(KeyCode.F)){
            animator.SetBool("isAttack", true);
        }
        if(Input.GetKeyUp(KeyCode.F)){
            animator.SetBool("isAttack", false);
        }

    }
}
